package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	bad()
	ok()
}

func bad() {
	counter := 1

	for i := 0; i < 100000; i++ {
		go func() {
			counter++
		}()
	}

	<-time.After(time.Second)
	fmt.Println(counter)
}

func ok() {
	counter := 1
	var mu sync.Mutex
	for i := 0; i < 100000; i++ {
		go func() {
			mu.Lock()
			defer mu.Unlock()
			counter++
		}()
	}

	<-time.After(time.Second)
	fmt.Println(counter)
}
